<?php

class beritaTable extends Doctrine_Table
{
    public function getLatestBerita() {
        //$q = $this->activeBerita();
        $q = $this->beritaByAdmin();

        return $q->execute();
    }

    public function activeBerita(Doctrine_Query $q = null) {
        if(is_null($q)) {
            $q = Doctrine_Query::create()->from('berita b')
                                        ->leftJoin('b.User u');
        }

        $alias = $q->getRootAlias();

        $q->addOrderBy($alias . '.created_at DESC');

        return $q;
    }

    public function listBerita(){
        $q = Doctrine_Query::create()->from('berita b')
                                        ->leftJoin('b.User u');
        $alias = $q->getRootAlias();
        
        return $q->execute();
    }

    public function getListBerita(){
        $q = $this->createQuery('b') ;

        return $q->execute();
    }

    public function beritaByAdmin() {
        $q = $this->createQuery('b')
                ->leftJoin('b.user u')
                ->leftJoin('u.sfGuardUser sf')
                ->where('sf.app_user_id = ?', 1);

        return $q;
    }

    public function getBeritaById($id_berita) {
        $q = $this->createQuery('b')
                ->where('id = ?', $id_berita);
        return $q->execute();
    }
}
